package cosas;

  /* Copyright (c) 2011 the authors listed at the following URL, and/or
  2 the authors of referenced articles or incorporated external code:
  3 http://en.literateprograms.org/Generic_tree_(Java)?action=history&offset=20080112104412
  4 
  5 Permission is hereby granted, free of charge, to any person obtaining
  6 a copy of this software and associated documentation files (the
  7 "Software"), to deal in the Software without restriction, including
  8 without limitation the rights to use, copy, modify, merge, publish,
  9 distribute, sublicense, and/or sell copies of the Software, and to
 10 permit persons to whom the Software is furnished to do so, subject to
 11 the following conditions:
 12 
 13 The above copyright notice and this permission notice shall be
 14 included in all copies or substantial portions of the Software.
 15 
 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 23 
 24 Retrieved from: http://en.literateprograms.org/Generic_tree_(Java)?oldid=12057
 25 */

 import java.util.Arrays;

 import java.util.List;

 import java.util.ArrayList;

  
  abstract class Tree<E> {
  	public abstract <R> R visit(Visitor<? super E, R> v);
  
  	public interface Visitor<E, R> {
  		public R leaf(E elt);
  		public R branch(List<? extends R> children);
  	}
  
  	public static <T> Tree<T> branch(final List<? extends Tree<T>> children) {
  		// return anonymous subclass for a branch:

 		return new Tree<T>() {
  			@Override
  			public <R> R visit(Visitor<? super T, R> v) {
  				List<R> result = new ArrayList<R>();
  				for (Tree<T> tree : children) {
  					result.add(tree.visit(v));
  				}
  				return v.branch(result);
  			}
  		};
  	}
  
  	public static <T> Tree<T> leaf(final T e) {
  		// return anonymous subclass for a leaf:

  		return new Tree<T>() {
  			@Override
  			public <R> R visit(Visitor<? super T, R> v) {
  				return v.leaf(e);
  			}
  		};
  	}
  
  	static class TreeClient {
  		public static String toString(Tree<?> t) {
  			// return an anonymous implementation of a visitor:

  			return t.visit(new Tree.Visitor<Object, String>() {
  				public String leaf(Object e) {
  					return e.toString();
  				}
  				public String branch(List<? extends String> children) {
  					String result = "(";
  					for (String c : children) {
  						result += c + " ";
  					}
  					return result.trim() + ")";
  				}
  			});
  		}
  
  
  		public static double sum(Tree<? extends Number> t) {
  			// return an anonymous implementation of a visitor:

  			return t.visit(new Tree.Visitor<Number, Double>() {
  				public Double leaf(Number e) {
  					return e.doubleValue();
  				}
  				public Double branch(List<? extends Double> children) {
  					Double res = 0.0;
  					for (Double d : children) {
  						res += d;
  					}
  					return res;
  				}
  			});
  		}
  
  		public static void main(String[] args) {
  			Tree<Integer> t = Tree.branch(Arrays.asList(Tree.branch(Arrays
  					.asList(Tree.leaf(1), Tree.leaf(2))), Tree.leaf(3)));
 			System.out.println(toString(t)); // --> ((1,2),3)

 			System.out.println(sum(t)); // --> 6.0

 		}
 
 	}
 }